package cn.gok.front.controller;

import cn.gok.front.service.IUserService;
import cn.gok.gok_mall.entity.User;
import cn.gok.gok_mall.responseData.ResponseCode;
import cn.gok.gok_mall.responseData.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Author: chenfan
 * @Description:
 */
@RestController
@RequestMapping("/user")
@Api(value = "前台用户接口类")
public class UserController {
    @Autowired
    IUserService userService;

    @ApiOperation(value = "前台用户登录的方法",notes = "用户根据用户名和密码进行登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",defaultValue = "zhangsan",dataType = "string"),
            @ApiImplicitParam(name = "password",value = "用密码",defaultValue = "123456",dataType = "string")
    })
    @PostMapping("/login.do")
    public ResultData login(String username, String password,HttpSession session){
        ResultData resultData = userService.queryUserByUserNameAndPassword(username, password);

        //如果登录成功，则将用户的信息存入session中
        if (resultData.getCode() == ResponseCode.SUCCESS.getCode()){
            session.setAttribute("userInfo", resultData.getData());
        }
       return resultData;
    }


    @GetMapping("/get_user_info.do")
    public ResultData getUserInfo(HttpSession session){
        User userInfo = (User) session.getAttribute("userInfo");
        return ResultData.createData(userInfo);
    }


    @GetMapping("/update_information.do")
    public ResultData updateUserInfo(HttpSession session,User user){

        //从session中获取用户的基本信息
        User userInfo = (User) session.getAttribute("userInfo");

        //设置id
        user.setId(userInfo.getId());
        System.out.println("调修改用户信息的业务");

        System.out.println("修改成功");
        //需要将session中的用户信息更新
        session.setAttribute("userInfo", "新用户数据");

        return null;
    }



    @ApiOperation(value = "获取用户密保问题",notes = "用户忘记密码时，根据用户名获取用户的密码问题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",defaultValue = "zhangsan",dataType = "string")
    })
    @GetMapping("/forget_get_question.do")
    public ResultData getUserPasswordQuestion(String username){
        return userService.queryUserPasswordQuestion(username);
    }



    @ApiOperation(value = "检查用户密保答案是否正确",notes = "根据用户名、密保问题、答案检查用户输入是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",defaultValue = "zhangsan",dataType = "string"),
            @ApiImplicitParam(name = "question",value = "密保问题",dataType = "string"),
            @ApiImplicitParam(name = "answer",value = "答案",dataType = "string")
    })
    @GetMapping("/forget_check_answer.do")
    public ResultData checkPasswordAnswer(String username, String question ,String answer){
        return userService.checkUserPasswordAnswer(username,question,answer);
    }

}
